package aaaClicker;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
 
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import javax.imageio.ImageIO;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import aaaBrowser.CaptX;
import aaaBrowser.ClosureEmpty;
import aaaBrowser.Reg;
import aaaBrowser.atiBrowserExample;

import com.attilax.Closure;
import com.attilax.core;
import com.attilax.io.filex;
import com.attilax.io.pathx;
import com.attilax.lang.ClipboardUtil;

public class CaptT {
	
	public CaptT() {
		this.capt=new CaptX();
	}

	private final class myrun implements Runnable,IResult {
		
		public ResultObject rztObj;
		public myrun(Runnable runnable) {
			// TODO Auto-generated constructor stub
		}
		public myrun(Future<?> fut) {
			// TODO Auto-generated constructor stub
		}
		public myrun(ResultObject rzt2) {
			rztObj=rzt2;
		}
		@Override
		public void run() {
			busy_single = true;
			System.out.println("---goto url start");
			String urlgoto = "http://guang.zhe800.com/guangbaobao";
			urlgoto="http://out.zhe800.com/guang/p/deal/100040878";
		 //urlgoto = "http://www.baidu.com/s?wd=ip地址&ie=UTF-8";
		//	urlgoto="http://atidestoon.com/";
			atiBrowserExampleinstance.gotoUrl(urlgoto);
			atiBrowserExampleinstance.addProgressListener(
					pathx.classPath(CaptT.class) + "\\capt_get.js",
					new Closure() {

						@Override
						public Object execute(Object arg0) throws Exception {
							// pub();
							Thread.sleep(1000);
							busy = false;
							busy_single = false;
							rztObj.rzt="ok";
							return null;
						}

					});
			atiBrowserExampleinstance.regJsCallbackMeth("getCaptch",
					new Closure() {

						@Override
						public Object execute(Object arg0) throws Exception {
							// setCaptch ();
							 String captchCode =capt.getTxt();
								
							 
							
var js="document.getElementByName('captcha')[0].value='"+captchCode+"';";
System.out.println("exe js:"+ js);
atiBrowserExampleinstance.browser.execute(js);
							return null;
						}
					});
			atiBrowserExampleinstance.gotoUrl();
			System.out.println("---goto url end");

		}
	}
	CaptX capt;
	public static void main(String[] args) {

		// execMeth_AysGui(new Runnable() {
		//
		// @Override
		// public void run() {
		//
		//
		// }
		// }, "");

		new CaptT().start();

	}

	final static Display display = new Display();
	atiBrowserExample atiBrowserExampleinstance;
	public static boolean busy = false;
	Shell shellNow ;
	private void start() {
		System.out.println("start---");
		for (int i = 0; i < 1; i++) {
			while (true) {
				try {
					Thread.sleep(5000);
					System.out.println("---is busy..");
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (!busy) {
					break;
				}

			}

			busy = true;
			rzt =new ResultObject();

			Shell shell = new Shell(display);
			shell.setLayout(new FillLayout());
			shell.setText("Browser example");
			// shell.setVisible(false);
			// shell.setSize(1, 1);
			atiBrowserExampleinstance = new atiBrowserExample(shell);

			shell.open(); // shell.setVisible(false);
			login(shell);// syn exe
			shellNow=shell;
			// execMeth_AysGui(new Runnable() {
			//
			// @Override
			// public void run() {
			//
			//
			// }
			// }, "");

			// setProxy("14.29.80.34:80");

			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		// atiBrowserExampleinstance.ini();

		// atiBrowserExampleinstance.addProgressListener(pathx
		// .classPath(LoginAndPub.class) + "\\login.js");

		while (!shellNow.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		// icon.dispose();
		  atiBrowserExampleinstance.dispose();
		  display.dispose();
	}

	private void setProxy(String string) {
		String[] a = string.split(":");
		System.getProperties().put("proxySet", "true");
		String host = a[0].trim();
		System.getProperties().setProperty("http.proxyHost", host);
		String port = a[1].trim();
		System.getProperties().setProperty("http.proxyPort", port);

		System.setProperty("network.proxy.type", "1");
		System.setProperty("network.proxy.http", host);
		System.setProperty("network.proxy.http_port", port);

	}

	boolean busy_single = true;
	ResultObject rzt =new ResultObject();
	/**
	 * 账号100719000050 ，密码065355，网址http://fxs.gzmcc.net/Login.aspx
	 * 
	 * @param shell
	 */
	private void login(Shell shell) {
		// core.execMeth_Ays(new Runnable() {
		//
		// @Override
		// public void run() {
		System.out.println("=-ajsy");
		// todox p4n
		// yaosi cant exed ori :: display .syncExec to
		// display.getDefault().syncExec
		// display.getDefault().syncExec(new Runnable() {
		// @Override
		// public void run() {
		System.out.println(" start login evet..");
	
		final Future<?> fut = new FutureTaskAti<Object>(new Runnable() {

			@Override
			public void run() {
				System.out.println(" start asynexe goto thread");
				display.asyncExec(new myrun(rzt));

//				while (true) {
//					if (busy_single) { // Thread.yield();
//						System.out.println("--is true");
//
//						System.out.println("111");
////						display.syncExec(new Runnable() {
////
////							@Override
////							public void run() {
////								if (!display.readAndDispatch()) {
////									System.out.println("222");
////									display.sleep();
////								}
////
////							}
////						});
//
//						  try {
//							Thread.sleep(1000);
//						} catch (InterruptedException e) {
//							// TODO Auto-generated catch block
//							e.printStackTrace();
//						}
//
//						System.out.println(" busy single busy2");
//					} else
//						break;
//				}
			}
		}, rzt);
	
//		ExecutorService es = Executors.newSingleThreadExecutor();
//		Future<String> fut = es.submit(new Callable<String>() {
//
//			@Override
//			public String call() throws Exception {
//				System.out.println("--future call ");
//				new Thread() {
//					@Override
//					public void run() {
//						System.out.println(" start asynexe goto thread");
//						display.asyncExec(new myrun());
//						
//						
//						System.out.println(" --disp after");
//						
//						
//					}
//
//				}.start();
//			
//			
//				//
//				// //
//				// // execMeth_AysGui(new Runnable() {
//				// //
//				// // @Override
//				// // public void run() {
//				// //
//				// //
//				// // }
//				// // }, "");
//				//
//				return null;
//
//			}
//		});

		// Future<?> fut = new FutureTask<Object>(new Runnable() {
		//
		// @Override
		// public void run() {
		//
		// }
		// }, null);
 
		try {
			fut.get(30, TimeUnit.SECONDS);
			busy = false;
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			busy = false;
		}
	//	es.shutdown();
 
		System.out.println("-- login process finish");
	}

	public static void execMeth_AysGui(final Runnable runnable, String string) {
		core.execMeth_Ays(new Runnable() {

			@SuppressWarnings("static-access")
			@Override
			public void run() {
				System.out.println("=-ajsy");
				// Display default1 = display.getDefault();
				// runnable.run();
				Display default1 = Display.getDefault();
				System.out.println(runnable);
				display.asyncExec(runnable);
				// display.asyncExec(runnable);
			}
		}, string);

	}

}
